package com.example.opengldemo.simpleGame.util;

import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
import com.bulletphysics.linearmath.DefaultMotionState;
import com.bulletphysics.linearmath.Transform;

import javax.vecmath.Vector3f;

public class RigidBodyHelper {
	public static RigidBody addRigidBody(float mass, CollisionShape shape,
										 float cx, float cy, float cz, DiscreteDynamicsWorld dynamicsWorld, boolean noGravity)
    {
		
		CollisionShape comShape=shape;
  		boolean isDynamic = (mass != 0f);
		Vector3f localInertia = new Vector3f(0f, 0f, 0f);
		if (isDynamic) {
			comShape.calculateLocalInertia(mass, localInertia);
		}
		
		Transform startTransform = new Transform();
		startTransform.setIdentity();
		startTransform.origin.set(new Vector3f(cx, cy, cz));
		DefaultMotionState myMotionState = new DefaultMotionState(startTransform);
		RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, myMotionState,
				comShape, localInertia);
		RigidBody body = new RigidBody(rbInfo);
		body.setRestitution(0.0f);
		body.setFriction(0.2f);
		body.forceActivationState(RigidBody.DISABLE_DEACTIVATION);
		dynamicsWorld.addRigidBody(body);
		if(noGravity){
	 		body.setGravity(new Vector3f(0,0,0));
	 	}
		return body;
    }

}
